﻿//Ana Isabel Ibáñez García
//version 1: create toolStripButton with buttons save, create class, relations and export image
            //add the formControl
//version 2: create a classView when click in toolStripButton, at coord 100,100
            //method GetFormControl
//version 3: PaintRelation, changed toolStripButton1_Click to Add the new class to
//the list of classes in WindowsManager, btnAddRelation_Click
//version 4: changed: toolStripButton1_Click and PaintRelations. added methods OnMouseMoveHandler, OnClickEventHandler 
//mainPanel_Panel1_Paint,inheritanceMenuOption_Click, compositionToolStripMenuItem_Click,aggregationToolStripMenuItem_Click
//version 9: added exportToCode

using System;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing;



namespace UMLDesigner
{
    partial class MainWindow : Form
    {
        private Pen dashed = new Pen(Pens.Silver.Brush);
        public MainWindow()
        {
            InitializeComponent();
            dashed.DashStyle = DashStyle.Dash;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            mainPanel.Panel1.MouseMove += new MouseEventHandler(OnMouseMoveHandler);
            mainPanel.Panel1.MouseDown += new MouseEventHandler(OnClickEventHandler);
        }

        private void OnMouseMoveHandler(object sender, MouseEventArgs e)
        {
            Graphics g = mainPanel.Panel1.CreateGraphics();

            Pen pen = new Pen(Pens.Red.Brush);
            pen.DashStyle = DashStyle.Dash;

            PaintRelations();
            g.DrawLine(dashed, 0, e.Y, mainPanel.Panel1.Width, e.Y);
            g.DrawLine(dashed, e.X, 0, e.X, mainPanel.Panel1.Height);
        }

        private void OnClickEventHandler(object sender, MouseEventArgs e)
        {
            mainPanel.Panel1.MouseMove -= new MouseEventHandler(OnMouseMoveHandler);
            mainPanel.Panel1.MouseDown -= new MouseEventHandler(OnClickEventHandler);

            ClassView clase = new ClassView(e.X, e.Y);
            WindowsManager.AddClass(clase);
            mainPanel.Panel1.Controls.Add(clase.GetControl());

            //Repaint the relations
            PaintRelations();
        }

        public void DeleteClass(ClassView cv)
        {
            mainPanel.Panel1.Controls.Remove(cv.GetControl());
        }

        public FormControl GetControlFormulario()
        {
            return formControl;
        }
        public void PaintRelations()
        {
            Graphics g = mainPanel.Panel1.CreateGraphics();
            //clear the screen and activate antialiasing mode
            g.Clear(Color.White);
            g.SmoothingMode = SmoothingMode.AntiAlias;
           
            foreach (RelationView rel in WindowsManager.GetRelations())
            {
                rel.Paint(g);
            }
            
            g.Dispose();
        }

        private void mainPanel_Panel1_Paint(object sender, PaintEventArgs e)
        {
            
            PaintRelations();
        }
       
        private void btnExportToImage_Click(object sender, EventArgs e)
        {

        }

       
       
        private void inheritanceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WindowsManager.SetRelationToCreate(new InheritanceView());
        }

        private void compositionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WindowsManager.SetRelationToCreate(new CompositionView());
        }

        private void aggregationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WindowsManager.SetRelationToCreate(new AggregationView());
        }

        private void btnAddRelation_ButtonClick(object sender, EventArgs e)
        {
            
        }

        private void btnExportCode_Click(object sender, EventArgs e)
        {
            ExportCode export = new ExportCode();
            export.ShowDialog();
            // WindowsManager.ExportToCode("");
        }
       
    }
}
